package main;


import com.google.common.base.Preconditions;
import org.apache.commons.lang3.time.StopWatch;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;


/**
 * 读取文件线程
 */
class Producer implements Runnable {
    private LinkedBlockingQueue<File> queue;

    Producer(LinkedBlockingQueue<File> queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
        try {
            while (true) {
                Thread.sleep((long) (Math.random() * 10));
                //取出并移除队列头
                File head = this.queue.take();
                System.out.println(Thread.currentThread().getName() + "---" + head.getAbsolutePath());
                //如果是一个目录
                if (head.isDirectory()) {
                    File[] files;
                    try {
                        files = Preconditions.checkNotNull(head.listFiles());
                        List<File> list = Arrays.asList(files);
                        list.forEach(temp -> {
                            try {
                                this.queue.put(temp);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        });

                    } catch (NullPointerException e) {
                        System.out.println("files为空");
                    }
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}

/**
 * 列举指定路径下面的所有目录以及文件
 * User: R
 * Date: 2018/9/4
 * Time: 11:43
 * Created with IntelliJ IDEA.
 */
public class FindAllFile {
    public static void main(String[] args) throws InterruptedException {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        LinkedBlockingQueue<File> linkedBlockingQueue = new LinkedBlockingQueue<>();
        File file = new File("C:/");
        linkedBlockingQueue.put(file);
        Producer p1 = new Producer(linkedBlockingQueue);
        Producer p2 = new Producer(linkedBlockingQueue);
        Producer p3 = new Producer(linkedBlockingQueue);
        Producer p4 = new Producer(linkedBlockingQueue);
        ExecutorService service = Executors.newCachedThreadPool();
        service.execute(p1);
        service.execute(p2);
        service.execute(p3);
        service.execute(p4);
        System.out.println("总共耗时:" + stopWatch.toString());
        stopWatch.stop();
    }

//    public static void main(String[] args) throws InterruptedException {
//        StopWatch stopWatch = new StopWatch();
//        stopWatch.start();
//        LinkedBlockingQueue<File> linkedBlockingQueue = new LinkedBlockingQueue<>();
//        File file = new File("C:/");
//        linkedBlockingQueue.put(file);
//        while (!linkedBlockingQueue.isEmpty()) {
//            File head = linkedBlockingQueue.take();
//            System.out.println(Thread.currentThread().getName() + "---" + head.getAbsolutePath());
//            //如果是目录
//            if (head.isDirectory()) {
//                File[] files;
//                try {
//                    files = Preconditions.checkNotNull(head.listFiles());
//                    List<File> list = Arrays.asList(files);
//                    list.forEach(temp -> {
//                        try {
//                            linkedBlockingQueue.put(temp);
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
//                    });
//
//                } catch (NullPointerException e) {
//                    System.out.println("files为空");
//                }
//            }
//        }
//        System.out.println("总共耗时:" + stopWatch.toString());
//        stopWatch.stop();
//    }
}
